<?php

namespace App\Http\Controllers\Api;

use App\Http\Model\Feedback;
use App\Http\Model\Notes;
use App\Http\Model\User;
use App\Http\Model\UserChange;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Mail;

class MemController extends Controller
{
    public function reg(){
        $data['uname']=\request()->input('uname');
        $data['password']=\request()->input('password');
        $data['clientid']=\request()->input('clientid',"机型错误");
        $data['gender']=\request()->input('gender','0');
        $data['telphone']=\request()->input('telphone','未填写');
//        $data['birthday']=\request()->input('birthday');
        $data['birthday']=date('y-m-d');
        $data['email']=\request()->input('email','未填写');
        $data['pic']="images/default/logo_notepad.png";
        $user=new User();
        $validator=Validator::make($data,$user->rules(),$user->msg());
        if($validator->fails()){
            $over['success']='0';
            return $over;
        }else{
            $data['password']=md5($data['password']);
            $result=User::create($data);
            if($result){
                $over['success']='1';
                return $over;
            }else{
                $over['success']='0';
                return $over;
            }
        }
    }

    public function login(){
        $uname=\request()->input('uname');
        $password=\request()->input('password');

        $password=md5($password);

        $result=User::where(['uname'=>$uname,'password'=>$password])->first();
//        dump($result);
        if($result){
            $over['success']=$result['uid']+"";
            return $over;
        }else{
            $over['success']='0';
            return $over;
        }
    }
    public function index(){
        $uid=\request()->input('uid');
//        $notes=DB::table('notes')->where('uid',$uid)->get();
        $notes=Notes::where('uid','=',$uid)->get();
        return $notes;
    }

    public function getNote(){
        $nid=\request()->input('nid');
        $result=Notes::find($nid);
        return $result;
    }

    public function uploads(){
//        $data=Input::all();
        $data['uid']=\request()->input('uid');
        $data['tittle']=\request()->input('tittle');
        $data['content']=\request()->input('content');
        $result=Notes::create($data);
        if($result){
            $over['success']='1';
            return $over;
        }else{
            $over['success']='0';
            return $over;
        }
    }

    public function edit(){
        $tid=Input::get('nid');
        $type=Notes::find($tid);
        echo json_encode($type);
    }

    public function noteupdate(){
        $uid=\request()->input('uid');
        $data['nid']=\request()->input('nid');
        $data['tittle']=\request()->input('tittle');
        $data['content']=\request()->input('content');

        $notes=new Notes();
        $validator=Validator::make($data,$notes->rules(),$notes->msg());
        if($validator->fails()){
            $over['success']='0';
            return $over;
        }else{
            $result=Notes::updateOrCreate(array('uid' => $uid,'nid'=>$data['nid']), $data);
            if($result){
                $over['success']='1';
                return $over;
            }else{
                $over['success']='0';
                return $over;
            }
        }
    }

    public function notedel(){
        $id=\request()->input('nid');
        $re=Notes::where('nid',$id)->delete();
        if ($re){
            $date=[
                'success'=>"1"
            ];
        }else{
            $date=[
                'success'=>"0"
            ];
        }
        return $date;
    }

    public function getUserInfo(){
        $uid=\request()->input('uid');
        $result=User::where('uid',$uid)->first();
        if($result){
            $result['success']="1";
            return $result;
        }else{
            $over['success']='0';
            return $over;
        }

    }

    public function updateUser(){
        $uid=\request()->input('uid');
        $data['uname']=\request()->input('uname');
        $data['gender']=\request()->input('gender');
//        $data['birthday']=\request()->input('birthday');
        $data['telphone']=\request()->input('telphone','未填写');
        $data['email']=\request()->input('email',"未填写");

        $users = DB::table('users')
            ->where('uid', '<>', $uid)->where('uname',$data['uname'])
            ->count();

        $user=new UserChange();
        $validator=Validator::make($data,$user->rules(),$user->msg());
        if(!$users){
            if($validator->fails()){
                $over['success']='0';
                $over['msg']=$validator->errors()->first();
                return $over;
            }else{
                $result=UserChange::where('uid',$uid)->update($data);
                if($result){
                    $over['success']='1';
                    return $over;
                }else{
                    $over['success']='0';
                    $over['msg']='无新数据上传';
                    return $over;
                }
            }
        }else{
            $over['success']='0';
            $over['msg']='用户名已存在';
            return $over;
        }
    }

    public function changePass(){
        $uid=\request()->input('uid');
        //原密码
        $password=\request()->input('password');
        $password=md5($password);
        //新密码
        $data['password']=\request()->input('newpass');
        $data['password']=md5($data['password']);
        $exist=User::where('uid',$uid)->where('password',$password)->count();
        if($exist){
            $result=User::where('uid',$uid)->update($data);
            if($result){
                $over['success']='1';
                return $over;
            }else{
                $over['success']='0';
                return $over;
            }
        }else{
            $over['success']='0';
            return $over;
        }
    }

    public function updatePortait(){
        $img=\request()->input('portrait');
        $data['pic']=$img;
        $id=\request()->input('uid');
        $exist=User::where('uid',$id)->count();
        if($exist){
            $exc=User::where('uid',$id)->update($data);
            if($exc) {
                $result = array("success" => '1');
            } else{
                $result = array("success" => '0');
            }
        }else{
            $result = array("success" => '0');
        }
        echo json_encode($result);
    }

    public function getVersion(){
        echo phpversion();
    }
    public function feedback(){
        $data['uid']=\request()->input('uid');
        $data['fbcontent']=\request()->input('feedback');

        $result=Feedback::create($data);
        if($result){
            $over['success']='1';
            return $over;
        }else{
            $over['success']='0';
            return $over;
        }
    }

    public function mail(){
        Mail::raw('你好BNP',function ($msg){
            $msg->from('446634431@qq.com','BNP');
            $msg->subject('验证码为。。。。。');
            $msg->to('18041162110@163.com');
        });
    }
}
